package com.tecode.myBooks.user.dao.impl;

import com.tecode.myBooks.user.bean.User;
import com.tecode.myBooks.user.dao.UserDao;
import com.tecode.myBooks.util.DBUtil;
import com.tecode.myBooks.util.DateUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;

/**
 * 
 * 处理用户相关的Dao层
 * @author Administrator
 *
 */
public class UserDaoImpl implements UserDao {

	@Override
	public User getUserByAccount(String account){
		//判断是否查询到信息
		//1.获得链接
		Connection conn =null;
		PreparedStatement ps =null;
		ResultSet rs = null;
		User u = null;
		try {
			//1.获得链接
			 conn = DBUtil.getConnection();
			
			//2.定义查询的SQL语句
			String sql ="select id,account,pwd,Nickname from user where account=? ";
			
			//3。创建执行SQL语句的预编译对象
			 ps = conn.prepareStatement(sql);
			
			//4.给占位符赋值
			ps.setString(1, account);
			
			//5.执行SQL语句
			 rs = ps.executeQuery();
			
			if(rs.next()){
				u = new User();
				
				u.setId(rs.getInt("id"));
				u.setAccount(rs.getString("account"));
				u.setPwd(rs.getString("pwd"));
				u.setNickname(rs.getString("Nickname"));
				
			}
		} catch (SQLException e) {			
			e.printStackTrace();
		}finally {
			DBUtil.close(conn, ps, rs);
		}
		
		
		return u;

	}


	/**
	 * 修改user表的信息
	 * @return
	 */
	@Override
	public int userChange(Integer code,String phone){


		Connection conn = DBUtil.getConnection();
		String sql="update user set veriCode=?,deadline=date_add(now(), interval 10 minute) where phone=?";
		/*Set<Map.Entry<String,Object>> fields = map.entrySet();

		for(Map.Entry<String,Object> entry :fields){
			sql +=entry.getKey() +"=?, ";
		}
		sql=sql.substring(0,sql.length()-1);

		sql =" where phone=?";

		PreparedStatement ps =  conn.prepareCall(sql);
		int index =1;

		for(Map.Entry<String,Object> entry :fields){
			ps.setObject(index++,entry.getValue());
		}

		ps.setObject(index,map.get("phone"));*/
		PreparedStatement ps = null;
		int row = 0;
		try {
			ps =  conn.prepareCall(sql);

			ps.setString(1,code+"");
			ps.setString(2,phone);

			row = ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(conn,ps,null);
		}

		return row;
	}

	/**
	 * 手机号码
	 * @param phone
	 * @return
	 */
	@Override
	public User phoneLogin(String phone) {

			Connection conn = DBUtil.getConnection();
			ResultSet rs = null;
		    PreparedStatement ps = null;
			String sql ="select id,nickname,phone,veriCode,deadline from user where phone=?";
		User user = null;
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1,phone);

			rs = ps.executeQuery();


			if(rs.next()){
					user = new User();
				user.setId(rs.getInt("ID"));
				user.setNickname(rs.getString("nickname"));
				user.setPhone(rs.getString("phone"));
				user.setVeriCode(rs.getString("veriCode"));
				String deadline = rs.getString("deadline");
				System.out.println(deadline);
				try {
					user.setDeadline(DateUtil.StringToDate(deadline,"yyyy-MM-dd HH:mm:ss.SSS"));
				} catch (ParseException e) {
					e.printStackTrace();
				}



			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(conn,ps,rs);
		}

		return user;
	}

}
